/* See LICENSE for copyright and terms of use */

import org.as2lib.test.unit.TestCase;
import org.evtmng.util.List;
import org.actionstep.constants.NSComparisonResult;

/**
 * @author Tay Ray Chuan
 */

class org.evtmng.test.unit.ListTest extends TestCase {
	private static var GAP:Number = 17;

	private var m_list:List;
	private var m_ctr:Number;

	public function setUp():Void {
		m_list = new List();
		m_ctr = 0;
	}

	public function testLengthIntegrity():Void {
		var i:Number, arr:Array;

		//control variable to benchmark List integrity against
		m_list.removeAllItems();
		arr = ["foo", "hello world", "another item", 1000, 162, "gross", "it's a bird", "it's a plane"];
		i = arr.length;
		while(i--) {
			m_list.addItemWithId(arr[i], m_ctr+=GAP);
		}
		assertEquals(arr.length, m_list.length());

		var kill:Array = ["another item", 162, "foo"];
		for(var x:String in kill) {
			for(i = arr.length;i>=0;i--) {
				if(arr[i] == kill[x]) {
					arr.splice(i, 1);
				}
			}
			m_list.removeItem(kill[x]);
		}
		assertEquals(arr.length, m_list.length());
	}

	public function testItemDuplicity():Void {
		var i:Number, $I:Number;

		//$i is a constant
		m_list.removeAllItems();
		$I = i = 10;
		while(i--) {
			m_list.addItemWithId("foo", i);
		}

		var arr:Array = m_list.allIdsForItem("foo");
		assertEquals(arr.length, $I);
	}

	public function testAscendingListSort():Void {
		var test:List = new List();
		test.addItemWithId(1, 0);
		test.addItemWithId(3, 2);
		test.addItemWithId(8, 4);
		test.addItemWithId(5, 3);
		test.addItemWithId(2, 1);

		var count:Number;

		test.sortUsingFunction(function(a:Number, b:Number):Boolean {
			if(a<b) {
				return true;
			}
		});

		count=0;
		for(var i:String in test) {
			assertEqualsWithMessage("asc value", i, count);
			count++;
		}
	}

	public function testDescendingListSort():Void {
		var test:List = new List();
		test.addItemWithId(1, 0);
		test.addItemWithId(3, 2);
		test.addItemWithId(8, 4);
		test.addItemWithId(5, 3);
		test.addItemWithId(2, 1);

		var count:Number;

		test.sortUsingFunction(function(a:Number, b:Number):Boolean {
			if(a>b) {
				return true;
			}
		});

		count=test.length()-1;
		for(var i:String in test) {
			assertEqualsWithMessage("dsc value", i, count);
			count--;
		}
	}
}